/*=======================================================================================
	Heterogeneity.do
	
		Uses the Personal Finance Website (PFW) data to analyze the response of 
		consumption to the APFD payments across different subpopulations
		
	Author: Lorenz Kueng, June 2015
=========================================================================================*/

cap log close PFW_07b
log using "$homedir/log-files/PFW_07b_$date.log", text replace name(PFW_07b)


*=============================================================
* Data
*=============================================================

use "$homedir/data/stata/PFW_quarterly.dta", clear 

	generate date = Quarter
	format   date %tq
	
	lab var APFD "PFD payments" // APFD payments indentified via direct deposit entry in early October

	xtset userid date

	
*** Create additional variables

	** controls
	foreach var in residentialstatus marriagestatus {
		display _n(1) "`var'"
		cap drop `var'_
		encode   `var', generate(`var'_)
		bysort userid (date): egen _temp = mode(`var'_), maxmode
		replace  `var'_ = _temp if `var'_==. & _temp!=. 
		replace  `var'_ = 999   if `var'_==. 
		drop _temp
	}
	foreach var in age edu profession homezipcode {
		display _n(1) "`var'"
		cap drop `var'_
		generate `var'_ = `var'
		bysort userid (date): egen _temp = mode(`var'_), maxmode
		replace  `var'_ = _temp if `var'_==. & _temp!=. 
		replace  `var'_ = 999   if `var'_==. 
		drop _temp
	}
	
	cap drop _temp
	gen D_incomeQ = D.incomeQ
	bysort userid (Quarter): egen _temp = mean(D_incomeQ)
	replace D_incomeQ = _temp if missing(D_incomeQ)==1
	drop _temp*
	
	foreach var in incomeQ D_incomeQ incomeY liquid_asset1 {
		winsor `var', g(`var'W) p(0.01)
		sum `var' `var'W, d
	}



*=============================================================
* Create Quantiles
*=============================================================
		
	*----------------------------------
	* relative size of dividend
	*----------------------------------
	*
	*  NOTE: Use nominal values since these are ratios, hence no need for CPI

		** average quarterly total spending (nondurables, services, durables)
		bysort userid: egen totexp_annual_nominal = mean(totexp1_nominal) 

		** annualize
		replace   totexp_annual_nominal = -totexp_annual_nominal*4

		** winsor
		summarize totexp_annual_nominal, detail
		winsor    totexp_annual_nominal, p(0.01) generate(_temp)
		replace   totexp_annual_nominal = _temp
		summarize totexp_annual_nominal, detail
		drop _temp

		** relative dividend
		generate ShockSize = APFD_annual_nominal/totexp_annual_nominal
		replace  ShockSize = ShockSize * 0.76 if state=="WA" // attenuation factor 2010-2014
		
		** compute quantiles
		egen     ShockSize_quantile = xtile(ShockSize), n(4) by(year Alaska) // Note: by year and state since we have to impute PFD for users in Washington based on self-reported family size instead of number of observed PFD checks
		

	*----------------------------------
	* relative liquidity
	*----------------------------------

		** relative liquidity: bank balances relative to quarterly total spending
		bysort state: sum            liquid_asset1_nominal totexp_annual_nominal
		generate LiquidityRelative = liquid_asset1_nominal/(totexp_annual_nominal/4)
		
		** compute quantiles
		egen     LiquidityRelative_quantile = xtile(LiquidityRelative), n(4) by(year Alaska) 


*=============================================================
* Table:  MPC Heterogeneity - Liquidity vs Size
*=============================================================

sort userid date

lab var APFD "PFD payments"

*
foreach depvar in nondur_serv {

	global depvar = "`depvar'"
	

	cap log close D${depvar}_heterog
	log using       "$homedir/results/tables/Table5_D${depvar}_Heterogeneity_Size_vs_Liquidity_${date}.log", text replace name(D${depvar}_heterog)

		global file "$homedir/results/tables/Table5_D${depvar}_Heterogeneity_Size_vs_Liquidity_${date}"

		cap rm "${file}.xls"
		cap rm "${file}.txt"
		
		xtset userid date


		*-----------------------------------------------------------
		* Winsorize dependent variable at 1%
		*-----------------------------------------------------------

		* save a copy of the original variable

		qui cap drop ${depvar}_orig
		qui generate ${depvar}_orig = ${depvar}
		
		* winsorize at 1% (conditional on non-zero observations for disaggregated categories)
		
		count if ${depvar}==0 
		
		if `r(N)'<5000 {
		
			cap drop temp
			winsor ${depvar}, generate(temp) p(0.01) 
			replace temp = 0 if temp==.
			replace ${depvar} = temp
		}
		else { // winsorize conditional on non-zeros for disaggregated variables
		
			cap drop temp
			winsor ${depvar} if ${depvar}!=0, generate(temp) p(0.01) 
			replace temp = 0 if temp==.
			replace ${depvar} = temp
		}
		
		* make all variables positive (i.e., expenditures)
		
		qui replace $depvar = abs(${depvar}) 
		summarize ${depvar}, detail

		
		*** Impose same sample in all regressions
		
			reghdfe ${depvar} APFD ///
					family_sizeImp ///
					liquid_asset1 ///
					incomeY ///
					D_incomeQ ///
				if APFDid_annual!=. /// NOTE: APFDid_annual!=. conditions on Alaskans for whom we know they received a dividend via direct deposit in early October
				, absorb(date userid marriagestatus_ residentialstatus_ age_ edu_ profession_ homezipcode_) vce(cluster userid)
	
			cap drop _e_sample
			generate _e_sample = e(sample)

				

		*--------------------------------------------------------------------------
		* By relative liquidity and relative shock size
		*--------------------------------------------------------------------------

			**Quantiles
			cap drop LiquidityRelative 
			generate LiquidityRelative = (liquid_asset1_nominal)/(totexp_annual_nominal/4)

			cap drop LiquidityRelative_Q
			egen LiquidityRelative_Q = xtile(LiquidityRelative), n(4) by(year Alaska) 

			cap drop APFD_ShockSize*_Liquidity*
			forvalues liq=1/4{
			forvalues size=1/4{
				generate APFD_ShockSize`size'_Liquidity`liq' = (ShockSize_quantile==`size')*(LiquidityRelative_Q==`liq')*APFD
			}
			}
		
			**Regression
			reghdfe D.${depvar}  APFD_ShockSize*_Liquidity* /// 
				D_incomeQ ///
				if _e_sample==1 ///
				, absorb(date Alaska family_sizeImp age_ edu_ marriagestatus_ residentialstatus_ profession_ homezipcode_ ShockSize_quantile LiquidityRelative_Q) vce(cluster userid)			
			
				outreg2 using "${file}.xls",  alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(3) se nocons label addstat(Number of clusters, e(N_clust)) ///
					ctitle("Size vs Liquidity") ///
					addtext( ///
							" - Time FE", Yes, /// 
							" - Family size FE", Yes, ///
							" - State FE", Yes, /// 
							" - Cash-on-hand liquidity ratio FE", Yes, ///
							" - Relative size FE", Yes, ///
							" - Income change", Yes, ///
							" - Household characteristics", Yes ///
					) ///
					keep( APFD_ShockSize*_Liquidity* )
			
			**Test statistics

			test APFD_ShockSize1_Liquidity1==APFD_ShockSize1_Liquidity4				
			test APFD_ShockSize2_Liquidity1==APFD_ShockSize2_Liquidity4	
			test APFD_ShockSize3_Liquidity1==APFD_ShockSize3_Liquidity4	
			test APFD_ShockSize4_Liquidity1==APFD_ShockSize4_Liquidity4	

			test APFD_ShockSize1_Liquidity1==APFD_ShockSize4_Liquidity1	
			test APFD_ShockSize1_Liquidity2==APFD_ShockSize4_Liquidity2	
			test APFD_ShockSize1_Liquidity3==APFD_ShockSize4_Liquidity3	
			test APFD_ShockSize1_Liquidity4==APFD_ShockSize4_Liquidity4	

			test APFD_ShockSize1_Liquidity1==APFD_ShockSize4_Liquidity4	
			test APFD_ShockSize4_Liquidity1==APFD_ShockSize1_Liquidity4	

	cap rm "${file}.txt"
}
					
log close PFW_07b
